package com.hsu.yupao.exception;

import com.hsu.yupao.common.BaseResponse;
import com.hsu.yupao.common.ErrorCode;
import com.hsu.yupao.common.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 * @ClassName: GlobalExceptionHandler
 * @author Hsu琛君珩
 * @date 2024-03-22
 * @apiNote
 * @Version: v1.0
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     *
     * @param e 自定义的业务异常
     * @return
     */
    @ExceptionHandler(BusinessException.class)
    public BaseResponse businessExceptionHandler(BusinessException e){
         log.error("businessException"+e.getMessage(),e);
         return ResultUtils.error(e.getCode(),e.getMessage(),e.getDescription());
    }

    /**
     *
     * @param e 运行时异常
     * @return
     */
    @ExceptionHandler(RuntimeException.class)
    public BaseResponse runtimeExceptionHandler(RuntimeException e){
        log.error("runtimeException",e);
        return ResultUtils.error(ErrorCode.SYSTEM_ERROR,e.getMessage(),"");
    }
}
